#include <iostream>
#include <cstdio>
#include <unistd.h>
#include "Comm.hpp"

#include <sys/select.h>

/*int main()
{
    std::cout << "fd_set: " << sizeof(fd_set) * 8 << std::endl;
    return 0;
}*/

int main()
{

    char buffer[1024];
    SetNonBlock(0);
    while(true)
    {
        ssize_t n = ::read(0, buffer, sizeof(buffer)-1);
        if(n > 0)
        {
            buffer[n] = 0;
            printf("echo# %s", buffer);
        }
        else if(n == 0)  // ctrl + d
        {
            printf("read done\n");
            break;
        }
        else
        {
            //底层数据没有就绪： errno 会被设置成为 EWOULDBLOCK EAGAIN
            if(errno == EWOULDBLOCK)
            {
                sleep(1);
                std::cout << "底层数据没有就绪，开始轮询检测" << std::endl;
                std::cout << "可以做其他事情" << std::endl;
                // do other thing
                continue;
            }
            else if(errno == EINTR)
            {
                continue;
            }
            else
            {
                perror("read");
                break;
            }
        }
    }

    return 0;
}